package com.sky.controller.mirror;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.entity.SubAccounts;
import com.sky.entity.WalletTransactions;
import com.sky.entity.Wallets;
import com.sky.result.Result;
import com.sky.service.SubAccountsService;
import com.sky.service.WalletTransactionsService;
import com.sky.service.WalletsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author GHOSTER by RemSynch
 * @since 2024-10-24
 */
@RestController
@RequestMapping("/admin/wallets")
@Slf4j
public class WalletsController {

    @Autowired
    private WalletsService walletsService;

    @Autowired
    private WalletTransactionsService walletTransactionsService;

    @Autowired
    private SubAccountsService subAccountsService;

    // 查询所有钱包
    @GetMapping
    public Result list() {
        return Result.success(walletsService.list());
    }

    // 查询所有钱包
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        Wallets wallets = walletsService.getById(id);
        log.info("wallets:{}", wallets);
        return Result.success(wallets);
    }

    @GetMapping("byAccountId/{id}")
    public Result getByAccountId(@PathVariable Long id) {
        LambdaQueryWrapper<Wallets> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Wallets::getUserId, id);
        Wallets wallet = walletsService.getOne(lambdaQueryWrapper);
        log.info("wallets:{}", wallet);
        return Result.success(wallet);
    }

    // 添加钱包
    @PostMapping
    public Result save(@RequestBody Wallets wallet) {
        return Result.status(walletsService.save(wallet));
    }

    // 更新钱包信息
    @PutMapping("/{id}")
    @Transactional
    public Result update(@PathVariable Long id, @RequestBody Wallets wallet) {
        wallet.setId(id);
        Wallets walletsInDB = walletsService.getById(id);
        if (walletsInDB == null) {
            return Result.error("钱包不存在");
        }
        BigDecimal change = wallet.getBalance().subtract(walletsInDB.getBalance());
        WalletTransactions walletTransactions = new WalletTransactions();
        walletTransactions.setUserId(wallet.getUserId());
        walletTransactions.setWalletId(id);
        walletTransactions.setBalance(wallet.getBalance());
        walletTransactions.setAmount(change);
        walletTransactions.setTransactionType(0);
        walletTransactions.setCreatedAt(LocalDateTime.now());
        walletTransactions.setDescription("后台赋值变动");
        return Result.status(walletsService.updateById(wallet) && walletTransactionsService.save(walletTransactions));
    }

    // 根据 ID 删除钱包
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        return Result.status(walletsService.removeById(id));
    }

    // 分页查询钱包列表
    @GetMapping("/page")
    public Result page(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Page<Wallets> walletPage = new Page<>(page, size);
        QueryWrapper<Wallets> queryWrapper = new QueryWrapper<>();
        return Result.success(walletsService.page(walletPage, queryWrapper));
    }

    @GetMapping("/getWithUser")
    public Result getWithUser() {
        return Result.success(walletsService.getWalletsByUser());
    }

}
